﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Aioms.Models;
using Furion.DynamicApiController;
using Furion.LinqBuilder;
using Furion.UnifyResult;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace Aioms.Controllers
{
    [DynamicApiController]
    public class MessageController
    {
        private readonly ISqlSugarClient db; // 核心对象：拥有完整的SqlSugar全部功能
        public MessageController(ISqlSugarClient sqlSugarClient)
        {
            this.db = sqlSugarClient;
        }

        /// <summary>
        /// 获取消息列表
        /// </summary>
        /// <param name="eid"></param>
        /// <param name="epid"></param>
        /// <param name="content"></param>
        /// <param name="type"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<IActionResult> PostList([FromForm] long eid, [FromForm] long epid, [FromForm] string content, [FromForm] string type, [FromForm] int pageIndex = 1, [FromForm] int pageSize = 10) {
            RefAsync<int> total = 0;
            List<Message> messages = await db.Queryable<Message>().Where(m => m.Eid == eid && m.Epid == epid)
                .WhereIF(!string.IsNullOrEmpty(type), m => m.Type == type)
                .WhereIF(!content.IsNullOrEmpty(), m => m.Name.Contains(content)).OrderBy(m => m.Id, OrderByType.Desc).ToPageListAsync(pageIndex, pageSize, total);
            UnifyContext.Fill(new { Total = total.Value });
            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = messages,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="eid"></param>
        /// <param name="epid"></param>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<IActionResult> PostDelete([FromForm] long eid, [FromForm] long epid, [FromForm] long[] ids)
        {
            if (ids.Length > 0) {
                await db.Deleteable<Message>().Where(m => m.Eid == eid && m.Epid == epid && ids.Contains(m.Id)).ExecuteCommandAsync();
            }
            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = "批量删除成功",
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        /// <summary>
        /// PC端-安卓端-批量设置已读
        /// </summary>
        /// <param name="eid"></param>
        /// <param name="epid"></param>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<IActionResult> PostShezhiyidu([FromForm] long eid, [FromForm] long epid, [FromForm] long[] ids)
        {
            if (ids.Length > 0)
            {
                await db.Updateable<Message>().Where(m => m.Eid == eid && m.Epid == epid && ids.Contains(m.Id)).SetColumns(m => m.Isread == 1).ExecuteCommandAsync();
            }
            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = null,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        /// <summary>
        /// PC端-安卓端-全部标记为已读
        /// </summary>
        /// <param name="eid"></param>
        /// <param name="epid"></param>
        /// <returns></returns>
        public async Task<IActionResult> PostQuanbubiaojiweiyidu([FromForm] long eid, [FromForm] long epid)
        {
            await db.Updateable<Message>().Where(m => m.Eid == eid && m.Epid == epid).SetColumns(m => m.Isread == 1).ExecuteCommandAsync();
            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = "全部标记为已读成功",
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        /// <summary>
        /// 安卓端-消息列表、未读数量接口
        /// </summary>
        /// <param name="eid"></param>
        /// <param name="epid"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<IActionResult> PostXiaoxishuliang([FromForm] long eid, [FromForm] long epid, [FromForm] int pageIndex = 1, [FromForm] int pageSize = 10)
        {
            RefAsync<int> total = 0;
            int wd_count = await db.Queryable<Message>().Where(m => m.Eid == eid && m.Epid == epid && m.Isread == 0).CountAsync();
            List<Message> messages = await db.Queryable<Message>().Where(m => m.Eid == eid && m.Epid == epid).ToPageListAsync(pageIndex, pageSize, total);
            UnifyContext.Fill(new { Total = total.Value, WeiDuCount = wd_count });
            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = messages,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }
    }
}
